﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ContaTelefone.Process;
using ContaTelefone.Entities;
using ContaTelefone.Reports;
using ContaTelefone.Data;

namespace ContaTelefone
{
    public partial class frmContas : Form
    {
        ContaProcess objProcess = new ContaProcess();

        public frmContas()
        {
            InitializeComponent();
            dgvRegistros.DataSource = objProcess.Listar();
        }

        private void tsbImportar_Click(object sender, EventArgs e)
        {
            frmImportarConta form = new frmImportarConta();
            form.ShowDialog();
        }

        private void tsbFechar_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvRegistros.SelectedRows.Count >= 1)
            {
                if (MessageBox.Show("Tem certeza que deseja excluir o registro selecionado?", "Conta Telefone", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ContaEntities objEtt = (ContaEntities)dgvRegistros.SelectedRows[0].DataBoundItem;
                    objProcess.Delete(objEtt);
                    dgvRegistros.DataSource = objProcess.Listar();
                }
            }
        }

        private void tsbCCusto_Click(object sender, EventArgs e)
        {
            DataTable dt;
            DataSet ds = new DataSet();

            try
            {
                if (dgvRegistros.SelectedRows.Count >= 1)
                {
                    ContaEntities objEtt = (ContaEntities)dgvRegistros.SelectedRows[0].DataBoundItem;

                    ReportsRepository rptRepo = new ReportsRepository();
                    dt = rptRepo.getContaPorCCus(objEtt.ID);
                    dt.TableName = "VW_VALOR_CCUS";
                    ValorPorCCus report = new ValorPorCCus();
                    ds.Tables.Add(dt);
                    ds.Tables.Add(PreencheLogo());
                    report.SetDataSource(ds);

                    frmPreview form = new frmPreview(report);
                    form.WindowState = FormWindowState.Maximized;
                    form.ShowDialog();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbRelPorNumero_Click(object sender, EventArgs e)
        {
            DataTable dt;
            DataSet ds = new DataSet();

            try
            {
                if (dgvRegistros.SelectedRows.Count >= 1)
                {
                    ContaEntities objEtt = (ContaEntities)dgvRegistros.SelectedRows[0].DataBoundItem;

                    ReportsRepository rptRepo = new ReportsRepository();
                    dt = rptRepo.getConta(objEtt.ID);
                    dt.TableName = "VW_CONTA";
                    Reports.ContaTelefone report = new Reports.ContaTelefone();
                    ds.Tables.Add(dt);
                    ds.Tables.Add(PreencheLogo());
                    report.SetDataSource(ds);

                    frmPreview form = new frmPreview(report);
                    form.WindowState = FormWindowState.Maximized;
                    form.ShowDialog();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbEnviar_Click(object sender, EventArgs e)
        {
            if (dgvRegistros.SelectedRows.Count >= 1)
            {
                ContaEntities objEtt = (ContaEntities)dgvRegistros.SelectedRows[0].DataBoundItem;

                frmEnvio form = new frmEnvio(objEtt);
                form.ShowDialog();
            }
        }

        private DataTable PreencheLogo()
        {
            DataTable dt = new DataTable();
            DataRow dr ;
            System.Drawing.Bitmap convertImage;

            try
            {
                dt.TableName = "LOGO";
                dt.Columns.Add("IMG_LOGO", typeof(System.Byte[]));
                dr = dt.NewRow();

                try
                {
                    convertImage = new System.Drawing.Bitmap(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "logo.jpg"));
                }
                catch (Exception ex)
                {
                    convertImage = new System.Drawing.Bitmap(100, 40);
                    System.Drawing.Graphics grp = System.Drawing.Graphics.FromImage(convertImage);
                    grp.FillRectangle(Brushes.White, 0, 0, 100, 40);
                    convertImage = new Bitmap(100, 40, grp);
                }

            
            dr["IMG_LOGO"] = ImageToByteArray(convertImage);

            dt.Rows.Add(dr);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private Byte[] ImageToByteArray(System.Drawing.Image Imagem )
        {
        try
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (Imagem != null)
                Imagem.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            
            return ms.ToArray();
        }
        catch (Exception ex )
        {
            throw ex;
        }
        }

    }
}
